﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lidgren.Network;
using Model.LiveControl;
using Network;
using Network.Messages.FileTranser;

namespace Providers.FileTransfer.Client
{
    public class FileTransferClientProvider : Provider
    {
        public FileTransferClientProvider(NetworkPeer network) : base(network)
        {
        }

        public override void RegisterMessageHandlers()
        {
            Network.RegisterMessageHandler<NotifyNewFileCanCopyMessge>(OnNotifyNewFileCanCopy);
            Network.RegisterMessageHandler<ResponseBeginCopyFileMessage>(OnResponseBeginCopyFileMessageRecieved);
            Network.RegisterMessageHandler<ResponseEndCopyFileMessage>(OnResponseEndCopyFileMessageRecieved);
            Network.RegisterMessageHandler<ResponseCopyFileMessage>(OnresponseCopyFileMessageRecieved);

            Network.RegisterMessageHandler<NotifyNewTextCanPasteMessage>(RecievedNotifyNewTextCanPaste);
        }

        public event EventHandler<NotifyNewTextCanPasteMessage> OnNotifyNewTextCanPaste;
        private void RecievedNotifyNewTextCanPaste(MessageEventArgs<NotifyNewTextCanPasteMessage> obj)
        {
            OnNotifyNewTextCanPaste?.Invoke(this, obj.Message);
        }

        private void OnResponseEndCopyFileMessageRecieved(MessageEventArgs<ResponseEndCopyFileMessage> obj)
        {
            if (!_dicStreams.ContainsKey(obj.Message.Number))
                return;

            var tCilent = _dicStreams[obj.Message.Number];
            tCilent.End();
            _dicStreams.Remove(obj.Message.Number);
            OnCopyfileRecived?.Invoke(this,obj.Message);
        }

        private void OnresponseCopyFileMessageRecieved(MessageEventArgs<ResponseCopyFileMessage> obj)
        {
            if (!_dicStreams.ContainsKey(obj.Message.Number))
                return;
            var tCilent = _dicStreams[obj.Message.Number];
            tCilent.Write(obj.Message.TempBuffer);


        }

        private readonly Dictionary<uint, StreamingCilent> _dicStreams = new Dictionary<uint, StreamingCilent>(); 
        private void OnResponseBeginCopyFileMessageRecieved(MessageEventArgs<ResponseBeginCopyFileMessage> obj)
        {
            try
            {
                StreamingCilent cilent = new StreamingCilent(obj.Message.FilePath, obj.Message.Length);
                _dicStreams.Add(obj.Message.Number, cilent);
            }
            catch (Exception)
            {
                // todo 失败处理
            }

        }

        private MessageEventArgs<NotifyNewFileCanCopyMessge> _currentCanCopyMessge = null;

        private void OnNotifyNewFileCanCopy(MessageEventArgs<NotifyNewFileCanCopyMessge> obj)
        {
            _currentCanCopyMessge = obj;
            ServerCopyChanged?.Invoke(this, obj.Message);

        }
        #region 对外接口

        public void NotifyStartServerPasteAFile(string tempPath, string filePath)
        {
            RequsetCopyFileMessage tCopyFileRequest = new RequsetCopyFileMessage()
            {
                DirPath = tempPath,
                DirList = new List<string>(),
                FileList = new List<string>() { filePath },
            };
            Network.SendMessage(tCopyFileRequest, NetDeliveryMethod.ReliableOrdered, _currentCanCopyMessge.Connection);

        }
        /// <summary>
        /// 启动远程粘贴
        /// </summary>
        public void NotifyStartSeverPaste(string dir)
        {
            if (_currentCanCopyMessge != null)
            {
                RequsetCopyFileMessage tCopyFileRequest = new RequsetCopyFileMessage()
                {
                    DirPath = dir,
                    DirList = _currentCanCopyMessge.Message.DirList,
                    FileList = _currentCanCopyMessge.Message.FileList,
                };
                Network.SendMessage(tCopyFileRequest, NetDeliveryMethod.ReliableOrdered, _currentCanCopyMessge.Connection);
            }
        }

        /// <summary>
        /// 执行了本地复制，移除远程复制状态
        /// </summary>
        public void NativeCopyFired()
        {
            _currentCanCopyMessge = null;
        }

        /// <summary>
        /// 服务端发生了新的复制
        /// </summary>
        public event EventHandler<NotifyNewFileCanCopyMessge> ServerCopyChanged;

        public event EventHandler<ResponseEndCopyFileMessage> OnCopyfileRecived;

        #endregion

    }
}
